package net.gzw1993.runmate.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

import net.gzw1993.runmate.R;
import net.gzw1993.runmate.adapter.RecorderAdapter;
import net.gzw1993.runmate.bean.Recorder;
import net.gzw1993.runmate.bean.RecorderInfo;
import net.gzw1993.runmate.bean.UserInfo;
import net.gzw1993.runmate.constant.UrlInfo;

import java.util.List;

/**
 * 我的运动记录对应的Activity
 */
public class RecorderActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private UserInfo userInfo;
    private RecorderAdapter adapter;
    @ViewInject(R.id.recorder_lv_list)
    private ListView listView;
    @ViewInject(R.id.recorder_srl_refresh)
    private SwipeRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recorder);
        ViewUtils.inject(this);
        userInfo = (UserInfo) getIntent().getSerializableExtra(UserInfo.class.getName());
        adapter = new RecorderAdapter(this, userInfo);
        listView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(this);
        requestNetwork();
    }

    private void requestNetwork() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, UrlInfo.getMyRecorder(0),
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        RecorderInfo recorderInfo = JSON.parseObject(responseInfo.result, RecorderInfo.class);
                        adapter.addItems(recorderInfo.getContent());
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                    }
                });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_recorder, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRefresh() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, UrlInfo.getMyRecorder(0),
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        RecorderInfo recorderInfo = JSON.parseObject(responseInfo.result, RecorderInfo.class);
                        List<Recorder> recorders = recorderInfo.getContent();
                        if(!recorders.get(0).getId().equals(((Recorder)adapter.getItem(0)).getId())){
                            adapter.replace(recorders);
                        }
                        refreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                    }
                });
    }
}
